package net.zoneland.knowledge.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import net.zoneland.knowledge.constant.Constants;
import net.zoneland.knowledge.controller.bean.FootPrint;
import net.zoneland.knowledge.model.LoginUserPojo;
import net.zoneland.knowledge.model.ResponseResult;
import net.zoneland.knowledge.service.FootprintService;
import net.zoneland.knowledge.utils.LoginUtils;

/**
 * 我的足迹.
 */
@RequestMapping("/footprint")
@RestController
public class FootprintController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FootprintController.class);

    @Autowired
    private FootprintService footprintService;

    /**
     * 获取足迹.
     * @param request 请求.
     * @param time 查询时间
     * @return 操作结果
     */
    @GetMapping("/getFootprint")
    public ResponseResult getFootprint(final HttpServletRequest request, final String time) {
        final ResponseResult result = new ResponseResult();
        final LoginUserPojo user = LoginUtils.getCurrentUserByToken(request.getHeader(Constants.TOKEN_NAME));
        try {
            final List<FootPrint> footprint = footprintService.getFootprint(user.getUserID(), time);
            result.setData(footprint);
        } catch (Exception e) {
            LOGGER.error("足迹查询失败，user:{},time:{},error:", user.getUserID(), time, e);
            result.setResult("0");
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping("/saveFootprint")
    public ResponseResult saveFootprint(final HttpServletRequest request, @RequestBody final FootPrint footPrint) {
        final ResponseResult result = new ResponseResult();
        try {
            final LoginUserPojo loginUserPojo = LoginUtils.getCurrentUserByToken(request.getHeader(Constants.TOKEN_NAME));
            footprintService.saveFootprint(loginUserPojo, footPrint);
        } catch (Exception e) {
            LOGGER.error("足迹保存失败，foot:{}", footPrint.toString(), e);
            result.setResult("0");
            result.setMessage("足迹保存失败。");
        }
        return result;
    }
}
